//
//  xinchengVC.m
//  qulvyou
//
//  Created by mmyy on 15/10/20.
//  Copyright (c) 2015年 mmyy. All rights reserved.
//

#import "xinchengVC.h"
#import "xingchengItems.h"
#import "xingchengCell.h"
#import "WayPoints.h"
@interface xinchengVC ()<UITableViewDataSource, UITableViewDelegate>

@end

@implementation xinchengVC
{
    UIView *_headView;
    UITableView *_tableView;
    xingchengItems *_items;
    UIImageView *_bgImage;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor purpleColor];
    _bgImage = [loadImage loadImage];
    _bgImage.frame = CGRectMake((self.view.frame.size.width - 100) / 2.0, (self.view.frame.size.height - 100) / 2, 100, 100);
    [self.view addSubview:_bgImage];
    [self loadData];
    
    // Do any additional setup after loading the view.
}
-(void)loadData
{
    _bgImage.hidden = NO;
    [self.view bringSubviewToFront:_bgImage];
    NSLog(@"%@", [NSString stringWithFormat:xinchengUrl, self.vid]);
    [Funcation getWithURL:[NSString stringWithFormat:xinchengUrl, self.vid] success:^(id responseObject) {
        _bgImage.hidden = YES;
        NSLog(@"Success");
        _items = [xingchengItems xingchengItemsWithDic:responseObject];
        [self createTableView];
    } andFail:^(id error) {
        NSLog(@"Fail");
    }];

}
-(void)createHeadView
{
    NSLog(@"Success");
    _headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 240)];
    _headView.backgroundColor = [UIColor clearColor];
    _tableView.tableHeaderView = _headView;
    UIImageView *headImage = [UIImageView new];
    NSString *imageString = [[[[_items waypoints] objectAtIndex:0] objectAtIndex:0] photo];
    [headImage setImageWithURL:[NSURL URLWithString:imageString]];
    [_headView addSubview:headImage];
    [headImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_headView.top).offset(0);
        make.left.equalTo(_headView.left).offset(0);
        make.width.equalTo(_headView.width);
        make.height.equalTo(@150);
    }];
    UIImageView *aval_l = [UIImageView new];
    [aval_l setImageWithURL:[NSURL URLWithString:[_items avatar_l]]];
    aval_l.layer.cornerRadius = 20;
    aval_l.layer.masksToBounds = YES;
    aval_l.layer.borderColor = [UIColor whiteColor].CGColor;
    aval_l.layer.borderWidth = 2;
    [_headView addSubview:aval_l];
    [aval_l mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_headView.left).offset((_headView.frame.size.width - 40) / 2.0);
        make.top.equalTo(_headView.top).offset(130);
        make.width.equalTo(40);
        make.height.equalTo(40);
    }];
    
    UILabel *name = [UILabel new];
    name.text = _items.name;
    name.textColor = [UIColor darkGrayColor];
    name.alpha = 0.5;
    name.textAlignment = NSTextAlignmentCenter;
    name.backgroundColor = [UIColor clearColor];
    name.font = [UIFont systemFontOfSize:18];
    [_headView addSubview:name];
    [name mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(aval_l.bottom).offset(10);
        make.left.equalTo(_headView.left).offset(0);
        make.right.equalTo(_headView.right).offset(0);
        make.height.equalTo(@20);
    }];
    
    UILabel *lname = [UILabel new];
    lname.text = _items.lname;
    lname.textColor = [UIColor blackColor];
    lname.textAlignment = NSTextAlignmentCenter;
    lname.backgroundColor = [UIColor clearColor];
    lname.font = [UIFont boldSystemFontOfSize:22];
    [_headView addSubview:lname];
    [lname mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(name.bottom).offset(10);
        make.left.equalTo(_headView.left).offset(0);
        make.right.equalTo(_headView.right).offset(0);
        make.height.equalTo(@25);
    }];
    
}
-(void)createTableView
{

    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 65, self.view.frame.size.width, self.view.frame.size.height - 65) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.allowsSelection = NO;
    [self.view addSubview:_tableView];
    [self createHeadView];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _items.waypoints.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[_items.waypoints objectAtIndex:section] count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    xingchengCell *cell = (xingchengCell *)[tableView cellForRowAtIndexPath:indexPath];
    if (!cell) {
        cell = [[xingchengCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"basis_cell"];
    }
    WayPoints *points = [[[_items waypoints] objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    cell.bgView.backgroundColor = [UIColor whiteColor];
    [cell.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(cell.top).offset(0);
        make.left.equalTo(cell.left).offset(10);
        make.right.equalTo(cell.right).offset(-10);
        make.bottom.equalTo(cell.bottom).offset(-5);
    }];
    [cell.photo setImageWithURL:[NSURL URLWithString:points.photo] placeholderImage:[UIImage imageNamed:@"define"]];
    [cell.photo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(cell.bgView.top).equalTo(0);
        make.left.equalTo(cell.bgView.left).offset(0);
        make.right.equalTo(cell.bgView.right).offset(0);
        if ([points.h floatValue] == 0) {
            make.height.equalTo(@150);
        }else {
            CGFloat with = [[points w] floatValue];
            CGFloat height = [[points h] floatValue];
            CGFloat newWith = (CGFloat)with / (self.view.frame.size.width - 20);
            CGFloat imageH = height / newWith;
            make.height.equalTo(imageH);
            
        }
    }];
    cell.text.text = points.text;
    cell.text.font = [UIFont systemFontOfSize:18];
    cell.text.lineBreakMode = NSLineBreakByWordWrapping;
    cell.text.numberOfLines = 0;
    [cell.text mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(cell.photo.bottom).offset(5);
        make.left.equalTo(cell.bgView.left).offset(5);
        make.right.equalTo(cell.bgView.right).offset(-5);
        make.bottom.equalTo(cell.bgView.bottom).offset(-30);
        
    }];
    cell.local_time.text = points.local_time;
    cell.province.text = points.province;
    if ([cell.province.text length] == 0) {
        cell.province.hidden = YES;
    }
    return cell;

}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WayPoints *points = [[[_items waypoints] objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    if ([[points h] floatValue] != 0) {
        CGFloat with = [[points w] floatValue];
        CGFloat height = [[points h] floatValue];
        CGFloat newWith = (CGFloat)with / (self.view.frame.size.width - 20);
        CGFloat imageH = height / newWith;
//        CGFloat imageW = self.view.frame.size.width - 20;
        NSString *text = [points text];
        if ([text length] != 0) {
            NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
            paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
            
            NSDictionary *attrDict = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:18],
                                      NSFontAttributeName,
                                      paragraphStyle,
                                      NSParagraphStyleAttributeName,
                                      nil];
            CGRect expectedRect = [text boundingRectWithSize:CGSizeMake(self.view.frame.size.width - 30, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attrDict context:nil];
            CGSize expectedSize = expectedRect.size;
            
        
            return imageH + expectedSize.height + 40;
        }else return imageH + 40;
    }else{
        NSString *text = [points text];
        if ([text length] != 0) {
            
            NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
            paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
            
            NSDictionary *attrDict = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:15],
                                      NSFontAttributeName,
                                      paragraphStyle,
                                      NSParagraphStyleAttributeName,
                                      nil];
            CGRect expectedRect = [text boundingRectWithSize:CGSizeMake(self.view.frame.size.width - 30, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attrDict context:nil];
            CGSize expectedSize = expectedRect.size;
        
            return expectedSize.height + 40;
        }else return 40;
    }
    
//    return 0;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40.0f;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 30)];
    view.backgroundColor = [UIColor purpleColor];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 100, 30)];
    label.text = [NSString stringWithFormat:@"第%d天", (int)section + 1];
    label.font = [UIFont boldSystemFontOfSize:25];
    label.textColor = [UIColor whiteColor];
    label.adjustsFontSizeToFitWidth = YES;
    [view addSubview:label];
    //    label.text =
    
    return view;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
